//
//	 ______    ______    ______    
//	/\  __ \  /\  ___\  /\  ___\   
//	\ \  __<  \ \  __\_ \ \  __\_ 
//	 \ \_____\ \ \_____\ \ \_____\ 
//	  \/_____/  \/_____/  \/_____/ 
//
//	Powered by BeeFramework
//
//
//  CompleteUserInfoBoard.m
//  iHeima
//
//  Created by he chao on 6/11/14.
//  Copyright (c) 2014 leon. All rights reserved.
//

#import "CompleteUserInfoBoard.h"

#pragma mark -

@interface CompleteUserInfoBoard()
{
	//<#@private var#>
}
@end

@implementation CompleteUserInfoBoard
DEF_SIGNAL(BACK)
DEF_SIGNAL(DONE)
DEF_SIGNAL(AVATAR)
DEF_SIGNAL(CAMERA)
DEF_SIGNAL(LOCAL)

- (void)load
{
}

- (void)unload
{
}

#pragma mark - Signal

ON_CREATE_VIEWS( signal )
{
    [self loadContent];
}

ON_DELETE_VIEWS( signal )
{
}

ON_LAYOUT_VIEWS( signal )
{
}

ON_WILL_APPEAR( signal )
{
}

ON_DID_APPEAR( signal )
{
}

ON_WILL_DISAPPEAR( signal )
{
}

ON_DID_DISAPPEAR( signal )
{
}

ON_SIGNAL3( BeeUINavigationBar, LEFT_TOUCHED, signal )
{
}

ON_SIGNAL3( BeeUINavigationBar, RIGHT_TOUCHED, signal )
{
}

ON_SIGNAL2(CompleteUserInfoBoard, signal) {
    if ([signal is:CompleteUserInfoBoard.BACK]) {
        [self.stack popBoardAnimated:YES];
    }
    else if ([signal is:CompleteUserInfoBoard.DONE]) {
    }
    else if ([signal is:CompleteUserInfoBoard.AVATAR]) {
        BeeUIActionSheet *sheet = [BeeUIActionSheet spawn];
        sheet.title = @"请选择操作";
        [sheet addButtonTitle:@"拍照" signal:CompleteUserInfoBoard.CAMERA];
        [sheet addButtonTitle:@"从相册选择" signal:CompleteUserInfoBoard.LOCAL];
        [sheet addCancelTitle:@"取消"];
        [sheet showInViewController:self];
    }
    else if ([signal is:CompleteUserInfoBoard.CAMERA]) {
        UIImagePickerController * c = [[UIImagePickerController alloc] init];
        c.delegate = self;
        c.sourceType = UIImagePickerControllerSourceTypeCamera;
        c.allowsEditing = YES;
        [self presentViewController:c animated:YES completion:nil];
    }
    else if ([signal is:CompleteUserInfoBoard.LOCAL]) {
        UIImagePickerController * c = [[UIImagePickerController alloc] init];
        c.delegate = self;
        c.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        c.allowsEditing = YES;
        [self presentViewController:c animated:YES completion:nil];
    }
}

- (void)loadContent{
    self.view.backgroundImage = IMAGESTRING(@"background");
    
    myScrollView = [BeeUIScrollView spawn];
    myScrollView.frame = self.viewBound;
    [self.view addSubview:myScrollView];
    
    
    BaseButton *btnBack = [BaseButton initBaseBtn:IMAGESTRING(@"navi_back2") highlight:nil];
    btnBack.frame = CGRectMake(20, IOS7_OR_LATER?20:0, 44, 44);
    [btnBack addSignal:CompleteUserInfoBoard.BACK forControlEvents:UIControlEventTouchUpInside];
    [myScrollView addSubview:btnBack];
    
    BaseLabel *title = [BaseLabel initLabel:@"注册成功!请完善资料" font:FONT(18) color:[UIColor blackColor] textAlignment:NSTextAlignmentCenter];
    title.frame = CGRectMake(0, btnBack.top, self.viewWidth, 44);
    [myScrollView insertSubview:title belowSubview:btnBack];
    
    imgAvatar = [BeeUIImageView spawn];
    imgAvatar.frame = CGRectMake(0, 0, 85, 85);
    imgAvatar.contentMode = UIViewContentModeScaleToFill;
    imgAvatar.image = IMAGESTRING(@"signup_avatar");
    imgAvatar.center = CGPointMake(self.viewWidth/2.0, title.bottom+50);
    [imgAvatar makeTappable:CompleteUserInfoBoard.AVATAR];
    [myScrollView addSubview:imgAvatar];
    
    NSArray *titles = @[@"姓名",@"城市",@"行业",@"公司",@"职务"];
    for (int i = 0; i < 5; i++) {
        BaseLabel *title = [BaseLabel initLabel:titles[i] font:FONT(14) color:[UIColor blackColor] textAlignment:NSTextAlignmentCenter];
        title.frame = CGRectMake(0, 0, 40, 44);
        
        field[i] = [BeeUITextField spawn];
        field[i].frame = CGRectMake(45, 44*i+imgAvatar.bottom+10, self.viewWidth-2*45, 44);
        field[i].contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        field[i].font = FONT(14);
        field[i].leftViewMode = UITextFieldViewModeAlways;
        field[i].leftView = title;
        [myScrollView addSubview:field[i]];
        
        BeeUIImageView *line = [BeeUIImageView spawn];
        line.frame = CGRectMake(field[i].left, field[i].bottom, field[i].width, 0.5);
        line.backgroundColor = RGB(188, 188, 188);
        [myScrollView addSubview:line];
    }
    
    BaseButton *btn = [BaseButton initBaseBtn:IMAGESTRING(@"btn1") highlight:IMAGESTRING(@"btn1_pre") text:@"开始使用" textColor:[UIColor whiteColor] font:FONT(14)];
    btn.frame = CGRectMake(field[4].left, field[4].bottom+20, field[4].width, 42);
    [btn addSignal:CompleteUserInfoBoard.DONE forControlEvents:UIControlEventTouchUpInside];
    [myScrollView addSubview:btn];
}

#pragma mark - QBImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)imagePickerController didFinishPickingMediaWithInfo:(id)info
{
    
    UIImage * i = [info[@"UIImagePickerControllerEditedImage"] copy];
    if(i.size.width > 640)
    {
        i = [self scaleImage:i toScale:640/i.size.width];
    }
    
    imgAvatar.image = i;
    image = i;
    
    [self dismissViewControllerAnimated:YES completion:NULL];
    //[self sendImage:i];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)imagePickerController
{
    NSLog(@"Cancelled");
    
    [self dismissViewControllerAnimated:YES completion:NULL];
}

- (UIImage *)scaleImage:(UIImage *)image1 toScale:(float)scaleSize
{
    UIGraphicsBeginImageContext(CGSizeMake(image1.size.width * scaleSize, image1.size.height * scaleSize));
    [image1 drawInRect:CGRectMake(0, 0, image1.size.width * scaleSize, image1.size.height * scaleSize)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}


@end
